package com.ruge.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruge.mapper.SQLiteMapper;
import com.ruge.service.SQLiteService;
import com.ruge.util.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SQLiteServiceImpl implements SQLiteService {


    @Resource
    private SQLiteMapper sqLiteMapper;


    @Override
    public Map<String, Object> pageList(Map<String, String> params) {
        List<JSONObject> list = new ArrayList<>();

        String page = params.getOrDefault("page", "1");
        String rows = params.getOrDefault("rows", "20");

        List<JSONObject> list1 = sqLiteMapper.getBusinessDataList(params);
        list1.forEach(businessData -> {
            businessData.put("TYPE", "BUSINESS");
            params.put("JLBH", businessData.getString("JLBH"));
            businessData.put("LIST", sqLiteMapper.getBusinessStampList(params));
        });
        list.addAll(list1);

        List<JSONObject> list2 = sqLiteMapper.getCenterDataList(params);
        list2.forEach(businessData -> {
            businessData.put("TYPE", "CENTER");
            params.put("JLBH", businessData.getString("JLBH"));
            businessData.put("LIST", sqLiteMapper.getCenterStampList(params));
        });
        list.addAll(list2);

        List<JSONObject> list3 = sqLiteMapper.getReportDataList(params);
        list3.forEach(businessData -> {
            businessData.put("TYPE", "REPORT");
            params.put("JLBH", businessData.getString("JLBH"));
            businessData.put("LIST", sqLiteMapper.getReportStampList(params));
        });
        list.addAll(list3);

        List<JSONObject> list4 = sqLiteMapper.getReportBigdataDataList(params);
        list4.forEach(businessData -> {
            businessData.put("TYPE", "REPORTBIGDATA");
            params.put("JLBH", businessData.getString("JLBH"));
            businessData.put("LIST", sqLiteMapper.getReportBigdataStampList(params));
        });
        list.addAll(list4);

        PageInfo<JSONObject> pageInfo = PageHelperUtils.memoryPage(list, Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<>();
        map.put("data", pageInfo);
        map.put("msg", "查询成功");
        return map;
    }
}
